#ifndef SRC_LTUTIL_H_
#define SRC_LTUTIL_H_

#include <stdint.h>

/** 定义函数mMerge3，将3个mType合并为一个，合并的方法是使用两次mMerge2。

 这里的「Merge」可能是「求最大」「求最小」这类具有结合律的运算。 */
#define ltutil__Define_Merge3(M_Type, M_Merge3, M_Merge2)                      \
    static inline M_Type M_Merge3(M_Type a, M_Type b, M_Type c) {              \
        return M_Merge2(a, M_Merge2(b, c));                                    \
    }

/** 完全类似Define_Merge3，只是函数参数改成4个。*/
#define ltutil__Define_Merge4(M_Type, M_Merge4, M_Merge2)                      \
    static inline M_Type M_Merge4(M_Type a, M_Type b, M_Type c, M_Type d) {    \
        return M_Merge2(M_Merge2(a, b), M_Merge2(c, d));                       \
    }

// clang-format off
#define ltutil__Define_MinMax(M_Type, M_TypeName)                              \
    static inline M_Type ltutil_Min2_##M_TypeName(M_Type a, M_Type b) {        \
        return a < b ? a : b;                                                  \
    }                                                                          \
    static inline M_Type ltutil_Max2_##M_TypeName(M_Type a, M_Type b) {        \
        return a > b ? a : b;                                                  \
    }                                                                          \
    ltutil__Define_Merge3(M_Type, ltutil_Min3_##M_TypeName,                    \
                          ltutil_Min2_##M_TypeName)                            \
    ltutil__Define_Merge3(M_Type, ltutil_Max3_##M_TypeName,                    \
                          ltutil_Max2_##M_TypeName)                            \
    ltutil__Define_Merge4(M_Type, ltutil_Min4_##M_TypeName,                    \
                          ltutil_Min2_##M_TypeName)                            \
    ltutil__Define_Merge4(M_Type, ltutil_Max4_##M_TypeName,                    \
                          ltutil_Max2_##M_TypeName)

ltutil__Define_MinMax(uint32_t, U32)
ltutil__Define_MinMax(uint64_t, U64)
ltutil__Define_MinMax(int32_t, I32)
ltutil__Define_MinMax(int64_t, I64)
// clang-format on

#undef ltutil__Define_MinMax
#undef ltutil__Define_Merge3

#endif
